package com.anpy.album.controller;

import com.anpy.album.dto.CalendarEventDTO;
import com.anpy.album.service.CalendarEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 家庭日历事件控制器
 */
@RestController
@RequestMapping("/calendar")
public class CalendarEventController {

    @Autowired
    private CalendarEventService calendarEventService;

    /**
     * 创建日历事件
     *
     * @param eventDTO 日历事件DTO
     * @return 响应结果
     */
    @PostMapping("/create")
    public ResponseEntity<Map<String, Object>> createEvent(@RequestBody CalendarEventDTO eventDTO) {
        Map<String, Object> result = new HashMap<>();
        try {
            CalendarEventDTO createdEvent = calendarEventService.createEvent(eventDTO);
            result.put("code", "200");
            result.put("message", "创建日历事件成功");
            result.put("data", createdEvent);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", "500");
            result.put("message", "创建日历事件失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 更新日历事件
     *
     * @param eventDTO 日历事件DTO
     * @return 响应结果
     */
    @PostMapping("/update")
    public ResponseEntity<Map<String, Object>> updateEvent(@RequestBody CalendarEventDTO eventDTO) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (eventDTO.getId() == null) {
                result.put("code", "400");
                result.put("message", "事件ID不能为空");
                return ResponseEntity.ok(result);
            }
            
            CalendarEventDTO updatedEvent = calendarEventService.updateEvent(eventDTO);
            result.put("code", "200");
            result.put("message", "更新日历事件成功");
            result.put("data", updatedEvent);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", "500");
            result.put("message", "更新日历事件失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 删除日历事件
     *
     * @param id 事件ID
     * @return 响应结果
     */
    @PostMapping("/delete")
    public ResponseEntity<Map<String, Object>> deleteEvent(@RequestParam("id") Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = calendarEventService.deleteEvent(id);
            if (success) {
                result.put("code", "200");
                result.put("message", "删除日历事件成功");
            } else {
                result.put("code", "404");
                result.put("message", "日历事件不存在或删除失败");
            }
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", "500");
            result.put("message", "删除日历事件失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 根据ID获取日历事件
     *
     * @param id 事件ID
     * @return 响应结果
     */
    @GetMapping("/get")
    public ResponseEntity<Map<String, Object>> getEventById(@RequestParam("id") Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            CalendarEventDTO event = calendarEventService.getEventById(id);
            if (event != null) {
                result.put("code", "200");
                result.put("message", "获取日历事件成功");
                result.put("data", event);
            } else {
                result.put("code", "404");
                result.put("message", "日历事件不存在");
            }
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", "500");
            result.put("message", "获取日历事件失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 获取用户的所有日历事件
     *
     * @param userId 用户ID
     * @return 响应结果
     */
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getEventsByUserId(@RequestParam("userId") Long userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<CalendarEventDTO> events = calendarEventService.getEventsByUserId(userId);
            result.put("code", "200");
            result.put("message", "获取日历事件列表成功");
            result.put("data", events);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", "500");
            result.put("message", "获取日历事件列表失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 获取用户指定时间范围内的日历事件
     *
     * @param userId    用户ID
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 响应结果
     */
    @GetMapping("/list/time-range")
    public ResponseEntity<Map<String, Object>> getEventsByTimeRange(
            @RequestParam("userId") Long userId,
            @RequestParam("startTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam("endTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<CalendarEventDTO> events = calendarEventService.getEventsByUserIdAndTimeRange(userId, startTime, endTime);
            result.put("code", "200");
            result.put("message", "获取时间范围内的日历事件成功");
            result.put("data", events);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", "500");
            result.put("message", "获取时间范围内的日历事件失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 获取用户指定类型的日历事件
     *
     * @param userId    用户ID
     * @param eventType 事件类型
     * @return 响应结果
     */
    @GetMapping("/list/event-type")
    public ResponseEntity<Map<String, Object>> getEventsByEventType(
            @RequestParam("userId") Long userId,
            @RequestParam("eventType") Integer eventType) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<CalendarEventDTO> events = calendarEventService.getEventsByUserIdAndEventType(userId, eventType);
            result.put("code", "200");
            result.put("message", "获取指定类型的日历事件成功");
            result.put("data", events);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", "500");
            result.put("message", "获取指定类型的日历事件失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 获取用户指定日期的日历事件
     *
     * @param userId 用户ID
     * @param date   日期
     * @return 响应结果
     */
    @GetMapping("/list/date")
    public ResponseEntity<Map<String, Object>> getEventsByDate(
            @RequestParam("userId") Long userId,
            @RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<CalendarEventDTO> events = calendarEventService.getEventsByUserIdAndDate(userId, date);
            result.put("code", "200");
            result.put("message", "获取指定日期的日历事件成功");
            result.put("data", events);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", "500");
            result.put("message", "获取指定日期的日历事件失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }
}
